package com.uyayo.system.config.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CodeMultipleException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.config.entity.SysServer;
import com.uyayo.system.config.mapper.SysServerMapper;
import com.uyayo.system.config.model.SysServerCondition;
import com.uyayo.system.config.service.SysServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("sysServerService")
public class SysServerServiceImpl implements SysServerService {

	private final SysServerMapper sysServerMapper;

	@Autowired
	public SysServerServiceImpl(SysServerMapper sysServerMapper) {
		this.sysServerMapper = sysServerMapper;
	}

	@Override
	public SysServer getSysServerById(long id){
		return sysServerMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageBean<SysServer> selectSysServer(Integer pageCurrent, Integer pageSize,String sort,String order,SysServerCondition sysServerCondition) {
		
		List<SysServer> sysServer;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("code", "code"));
		orderBies.add(new OrderBy("name", "name"));
		orderBies.add(new OrderBy("description", "description"));
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));
		orderBies.add(new OrderBy("remark", "remark"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		Example example = new Example(SysServer.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysServerCondition.getName())){
			criteria.andLike("name","%"+sysServerCondition.getName()+"%");
		}
		if(sysServerCondition.getBeginCreateTime()!=null || sysServerCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", sysServerCondition.getBeginCreateTime(), sysServerCondition.getEndCreateTime());
		}
		sysServer = sysServerMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<SysServer> pageInfo = new PageInfo<>(sysServer);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, sysServer);
	}
	
	@Override
	public List<SysServer> selectSysServer(SysServer sysServer) {
		return sysServerMapper.select(sysServer);
	}

	@Override
	public int saveSysServer(SysServer sysServer) throws CodeMultipleException {
        SysServer sysParamTest = getSysServerByCode(sysServer.getCode());
        if(sysParamTest!=null){
            throw new CodeMultipleException("Code multiple error!");
        }
		return sysServerMapper.insertSelective(sysServer);
	}

	@Override
	public int updateSysServer(SysServer sysServer) throws CodeMultipleException {
		SysServer sysSysServerTest = getSysServerByCode(sysServer.getCode());
		if(sysSysServerTest!=null && !Objects.equals(sysServer.getId(),sysSysServerTest.getId())){
			throw new CodeMultipleException("Code multiple error!");
		}
		return sysServerMapper.updateByPrimaryKeySelective(sysServer);
	}

	@Override
	public int deleteSysServerById(Long id) {
		return sysServerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public SysServer getSysServerByCode(String code) {
		SysServer sysServer = new SysServer();
		sysServer.setCode(code);
		sysServer = sysServerMapper.selectOne(sysServer);
		return sysServer;
	}

	@Override
	public SysServer getSysServerByEnabled(){
		SysServer sysServer = new SysServer();
		sysServer.setStatus(1);
		List<SysServer> Servers =  sysServerMapper.select(sysServer);
		return Servers.get(0);
	}
}
